package com.aaron.client;

import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Panel displayed in the upper right corner of the page.
 * Shows the currently logged in gmail email address of the user,
 * a help link, and a logout link
 * @author aaron
 *
 */
public class LinksPanel extends Composite {
	private Label user_label;
	public LinksPanel(String key){
		user_label=new Label("");
		
		HorizontalPanel panel=new HorizontalPanel();
		panel.setStyleName("vivcode-links-panel");
		panel.setSpacing(5);
		
		UserServiceAsync sessionService =UserService.Util.getInstance();
		sessionService.getUser(key, new AsyncCallback(){
			public void onSuccess(Object result){
				User user=(User)result;
				user_label.setText(user.getName()+"@gmail.com");
			}
			public void onFailure(Throwable caught) {
				user_label.setText("Failure:"+caught.getMessage());
			}
		});
		
		
		user_label.setStyleName("bold");
		panel.add(user_label);
		panel.add(new Label(" | "));
		if(VivCode.isEditing()){
			Hyperlink home_link=new Hyperlink("Projects Home","projects-home");
			home_link.addClickListener(new ClickListener(){
				public void onClick(Widget sender){
					VivCode.closeWindow();
				}
			});
			panel.add(home_link);
			panel.add(new Label(" | "));
		}
		panel.add(new Hyperlink("Help","help"));
		panel.add(new Label(" | "));
		Hyperlink signout_link=new Hyperlink("Signout","signout");
		signout_link.addClickListener(new ClickListener(){
			public void onClick(Widget sender){
				UserServiceAsync user_service=UserService.Util.getInstance();
				user_service.logout(VivCode.getAuth(), new AsyncCallback(){
					public void onSuccess(Object result){
						VivCode.redirectToHome();
					}
					public void onFailure(Throwable t){
						VivCode.redirectToHome();
					}
				});
			}
		});
		panel.add(signout_link);
		initWidget(panel);
	}
}
